package com.java.Apicontroller;


import cn.hutool.db.sql.Order;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.java.common.helper.JwtHelper;
import com.java.config.exceptioin.YyghException;
import com.java.config.result.Result;
import com.java.model.order.*;
import com.java.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.aspectj.lang.annotation.AfterThrowing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

//订单接口
@Api("订单")
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderApiController {

    @Autowired
    private OrderService orderService;

    //下单接口
    @ApiOperation("下单")
    @PostMapping("/auth/submitOrder/{scheduleId}/{patientId}")
    public Result submitOrder(@PathVariable String scheduleId, @PathVariable String patientId){
        if(patientId == null){
            throw new YyghException("请先确认就诊人信息",201);
        }
        Long orderId = orderService.saveOrder(scheduleId,patientId);
        return Result.ok(orderId);
    }

    //获取订单详情
    @ApiOperation("获取订单详情")
    @GetMapping("/auth/getOrders/{orderId}")
    public Result getOrders(@PathVariable String orderId){
        OrderInfo orderInfo = orderService.getOrders(orderId);
        return Result.ok(orderInfo);
    }


    //订单列表条件分页查询
    @ApiOperation("订单列表条件分页查询")
    @GetMapping("/auth/page/{pageNum}/{pageSize}")
    public Result selectByPage(
            @PathVariable Integer pageNum,
            @PathVariable Integer pageSize,
            OrderQueryVo orderQueryVo,
            HttpServletRequest request
    ){

        String token = request.getHeader("token");
        Long userId = JwtHelper.getUserId(token);
        Page<OrderInfo> page = new Page<>(pageNum,pageSize);
        IPage<OrderInfo> pages = orderService.selectByPage(page,orderQueryVo,userId);
        return Result.ok(pages);
    }

    //订单状态列表
    @ApiOperation("订单状态列表")
    @GetMapping("/auth/getStatusList")
    public Result getStatusList(){
        List<Map<String, Object>> statusList = OrderStatusEnum.getStatusList();
        return Result.ok(statusList);
    }

    //查询每月订单的次数
    @ApiOperation("查询每月订单的次数")
    @PostMapping("/auth/orderList")
    public Map<String,Object> getOrderList(@RequestBody OrderCountQueryVo orderCountQueryVo){
       return orderService.getOrderList(orderCountQueryVo);
    }

    //取消订单
    @ApiOperation("取消订单")
    @GetMapping("/auth/cancelOrder/{orderId}")
    public Result cancelOrder(@PathVariable Long orderId){
        Boolean cancelOrder = orderService.cancelOrder(orderId);
        return Result.ok(cancelOrder);
    }
}
